package com.example.ArrayList;

import java.util.ArrayList;
import java.util.Collection;

public class Test2Collection {
    public static void main(String[] args) {
        Collection<String> coll = new ArrayList<>();
        coll.add("aaa");
        coll.add("bbb");
        coll.add("ccc");
        coll.add("dddddd");
        //System.out.println(coll);

        //method1(coll);
        //method2(coll);
        //method3(coll);
        //method4(coll);
        //method5(coll);
        //method6(coll);

    }

    private static void method6(Collection<String> coll) {
        int size = coll.size();
        System.out.println(size);   //4
    }

    private static void method5(Collection<String> coll) {
        coll.clear();
        boolean empty = coll.isEmpty();
        System.out.println(empty);  //true
        System.out.println(coll);  //[]
    }

    private static void method4(Collection<String> coll) {
        boolean contains = coll.contains("aaa");
        boolean contains1 = coll.contains("d");
        System.out.println(contains); //true
        System.out.println(contains1); //false
    }

    private static void method3(Collection<String> coll) {
        coll.clear();
        System.out.println(coll);   //[]
    }

    private static void method2(Collection<String> coll) {
        // removeIf底层会遍历集合,得到集合中的每一个元素
        // s依次表示集合中的每一个元素
        // 就会把这每一个元素都到lambda表达式中去判断一下
        // 如果返回的是true,则删除
        // 如果返回的是false,则保留不删除

        // boolean test(T t);
        coll.removeIf((String s) -> {
            return s.length() == 3;
        });
        System.out.println(coll);   //[]
    }

    private static void method1(Collection coll) {
        boolean remove = coll.remove("aaa");
        boolean remove1 = coll.remove("ddd");
        System.out.println(remove); //true
        System.out.println(remove1);   //false
        System.out.println(coll);
    }
}
